﻿using CCWin.SkinControl;
using Mes.车间机床管理.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Web.Script.Serialization;
using System.Windows.Forms;

namespace Mes.车间机床管理
{
    public partial class WorkShopForm : Form
    {
        private readonly HttpClient _httpClient;
        private int index = 1;
        private int size = 15;
        private int totalPages = 1;
        public WorkShopForm()
        {
            InitializeComponent();
        }

        private void skinDataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            //自动根据数据源生成列
            skinDataGridView1.AutoGenerateColumns = false;
            skinDataGridView1.AllowUserToAddRows = false;
            skinDataGridView1.ReadOnly = false;
            if (skinDataGridView1.Columns["colIndex"]==null)
            {
                DataGridViewTextBoxColumn indexColumn = new DataGridViewTextBoxColumn()
                {
                    Name = "colIndex",
                    HeaderText = "序号",
                    ReadOnly = true
                };
                skinDataGridView1.Columns.Insert(0, indexColumn);
            }
            skinDataGridView1.Columns.Add(new DataGridViewTextBoxColumn
            {
                Name = "id",
                DataPropertyName = "id",
                HeaderText = "ID",
                Visible = false,
            });
            skinDataGridView1.Columns.Add(new DataGridViewTextBoxColumn
            {
                DataPropertyName= "workshopCode",
                HeaderText = "车间编号",
            });
            skinDataGridView1.Columns.Add(new DataGridViewTextBoxColumn
            {
                DataPropertyName = "workshopName",
                HeaderText = "车间名称",
            });
            skinDataGridView1.Columns.Add(new DataGridViewTextBoxColumn
            {
                DataPropertyName = "workshoplocation",
                HeaderText = "车间位置",
            });
            skinDataGridView1.Columns.Add(new DataGridViewTextBoxColumn
            {
                DataPropertyName = "workshopHead",
                HeaderText = "车间负责人",
            });
            skinDataGridView1.Columns.Add(new DataGridViewTextBoxColumn
            {
                DataPropertyName = "phone",
                HeaderText = "联系方式",
            });
            skinDataGridView1.Columns.Add(new DataGridViewTextBoxColumn
            {
                DataPropertyName = "state",
                HeaderText = "状态",
            });
           
        }
        /// <summary>
        /// 显示方法
        /// </summary>
        /// <returns></returns>
        private async Task GetTable()
        {
            try
            {
                var queryParams = new StringBuilder();
                queryParams.Append($"Index={index}");
                queryParams.Append($"Size={size}");
                if (!string.IsNullOrEmpty(textWorkshopName.Text))
                {
                    queryParams.Append($"&WorkshopName={textWorkshopName.Text.Trim()}");
                }
                if (!string.IsNullOrEmpty(textWorkshopCode.Text))
                {
                    queryParams.Append($"&WorkshopCode={textWorkshopCode.Text.Trim()}");
                }
                if (!string.IsNullOrEmpty(comWorkShopState.Text))
                {
                    queryParams.Append($"&State={comWorkShopState.Text.Trim()}");
                }

                string url = $"https://localhost:7084/api/WorkShop/GetWorkshop?{queryParams}";
                string json=await _httpClient.GetStringAsync(url);
                var serializer = new JavaScriptSerializer();
                var apiResponse = serializer.Deserialize<WorkshopRepository>(json);
                if (apiResponse?.code==200 && apiResponse.data?.datas!=null)
                {
                    totalPages = apiResponse.data.totalPage;
                    lblPageInfo.Text = $"当前页:{index}/总页数:{totalPages}";
                    skinDataGridView1.DataSource= apiResponse.data.datas;

                }
                else
                {
                    MessageBox.Show("数据加载失败");
                }
            }
            catch (Exception)
            {

                throw;
            }
        }
        private void butCha_Click(object sender, EventArgs e)
        {
            index = 1;
            GetTable();
        }

        private void btnShang_Click(object sender, EventArgs e)
        {
            if (index>1)
            {
                index--;
                GetTable();
            }
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            if (index< totalPages)
            {
                index++;
                GetTable();
            }
        }
        private void btnFrist_Click(object sender, EventArgs e)
        {
            index = 1;
            GetTable();
        }
        private void btnLast_Click(object sender, EventArgs e)
        {
            index = totalPages;
            GetTable();
        }

       
    }
}
